package edu.cs346.group5.ui;

import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import edu.cs346.group5.logic.Login;
import edu.cs346.group5.ui.adminpages.Categories;
import edu.cs346.group5.ui.forumpages.add_topic;
import edu.cs346.group5.ui.forumpages.search;
import edu.cs346.group5.ui.loginpages.login;
import edu.cs346.group5.ui.userpages.home;
import edu.cs346.group5.ui.userpages.user_page;

public class MenuFactory {
	public static JMenuBar getMenu(final JFrame frame) {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu mnHome = new JMenu("Home");
		menuBar.add(mnHome);
		
		JMenuItem mntmHome = new JMenuItem("Home");
		mnHome.add(mntmHome);
		mntmHome.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						home x = new home();
						x.setVisible(true);
						frame.dispose();
					}

				}
			);
		
		JMenuItem mntmProfilePage = new JMenuItem("Profile Page");
		mnHome.add(mntmProfilePage);
		mntmProfilePage.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					user_page x = new user_page(Login.LoggedInUser, true);
					x.setVisible(true);
					frame.dispose();
				}

			}
		);
		
		JMenuItem mntmAddTopicPage = new JMenuItem("Add Topic");
		mnHome.add(mntmAddTopicPage);
		mntmAddTopicPage.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					add_topic x = new add_topic();
					x.setVisible(true);
					frame.dispose();
				}

			}
		);
		
		JMenu mnSearchbrowse = new JMenu("Search");
		menuBar.add(mnSearchbrowse);
		
		
		JMenuItem mntmSearch = new JMenuItem("Search");
		mnSearchbrowse.add(mntmSearch);
		mntmSearch.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				search x = new search();
				x.setVisible(true);
				frame.dispose();
			}
		});
		
		JMenu mnV = new JMenu("V");
		menuBar.add(mnV);
		
		
		JMenuItem mntmLogOut = new JMenuItem("Log Out");
		mnV.add(mntmLogOut);
		mntmLogOut.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					login x = new login();
					x.setVisible(true);
					Login.LoggedInUser = null;
					frame.dispose();
				}

			}
		);
		
		if(Login.LoggedInUser.isAdmin) {
			
			JMenu mnAdmin = new JMenu("Admin");
			menuBar.add(mnAdmin);
			
			JMenuItem mntmCatMan = new JMenuItem("Category Management");
			mnAdmin.add(mntmCatMan);
			mntmCatMan.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						Categories x = new Categories();
						x.setVisible(true);
						frame.dispose();
					}
	
				}
			);
		}
		return menuBar;
	}
}
